package com.egg.ylt.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.egg.ylt.R;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.activity.ACT_ServiceItem;
import com.egg.ylt.pojo.ShopServerByIdEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ADA_OtherServiceAdapter extends RecyclerView.Adapter<ADA_OtherServiceAdapter.OtherServiceViewHolder> {
    private Context mContext;

    private String mShopId;
    private String mStorePhone;
    private List<ShopServerByIdEntity.ServiceListEntity> mServiceList;

    public ADA_OtherServiceAdapter(List<ShopServerByIdEntity.ServiceListEntity> serviceList, String shopId,
                                   String storePhone) {
        this.mServiceList = serviceList;
        mShopId = shopId;
        mStorePhone = storePhone;
    }

    @Override
    public OtherServiceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        return new OtherServiceViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_shop_detail_service,
                parent,
                false));
    }

    @Override
    public void onBindViewHolder(OtherServiceViewHolder holder, int position) {
        holder.refreshData(position);
    }

    @Override
    public int getItemCount() {
        return mServiceList == null ? 0 : mServiceList.size();
    }

    class OtherServiceViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_service_logo)
        ImageView mServiceLogo;

        @BindView(R.id.tv_service_name)
        TextView mServiceNameTv;

        @BindView(R.id.tv_service_feature)
        TextView mServiceContentTv;

        @BindView(R.id.tv_service_appoint)
        TextView mReservedCountTv;

        public OtherServiceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void refreshData(int position) {

            final ShopServerByIdEntity.ServiceListEntity dataEntity = mServiceList.get(position);

            Glide.with(mContext)
                    .load(dataEntity.getImageUrl())
                    .fallback(R.mipmap.ic_serve_default)
                    .dontAnimate()
                    .error(R.mipmap.ic_serve_default)
                    .into(mServiceLogo);

            mServiceNameTv.setText(dataEntity.getName());

            mServiceContentTv.setText(dataEntity.getInfo());

            mReservedCountTv.setText("已预约" + dataEntity.getReserveNum());

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ACT_ServiceItem.class);
                    intent.putExtra("id", dataEntity.getId());
                    intent.putExtra("shopId", mShopId);
                    intent.putExtra("storePhone", mStorePhone);
                    intent.putExtra("serviceImageUrl", dataEntity.getImageUrl());
                    mContext.startActivity(intent);
                }
            });

        }
    }
}
